<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>传送入口</title>

    <link rel="stylesheet" href="css/loginbackground.css">
    <link rel="stylesheet" href="css/login.css">

</head>

<body>
    <div class="nav">
        <img src="pic/miebao.jpg" alt="">
        <span class="blog-title">正在疯狂登录中...</span>
        <div class="space"></div>
        <a class="nav-span" href="blog_list.html">主页</a>
        <a class="nav-span" href="blog_edit.html">写博客</a>
    </div>

    <div class="container-login">
        <div class="login-dialog">
            <h3>登录</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" name="username" id="username">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" name="password" id="password">
            </div>
            <div class="row">
                <button id="submit" onclick="login()">提交</button>
            </div>

<!--            <div class="row">-->
<!--                <button id="register" onclick="register.html">新用户注册</button>-->
<!--            </div>-->
            <div style="text-align:center;"><a href="register.html" class="register-btn" >新用户注册</a></div>



        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script>
        function login() {
            $.ajax({
                type: "post",
                url: "/user/login",
                contentType: "application/json",
                data: JSON.stringify({
                    "userName": $("#username").val(),
                    "password": $("#password").val()
                }),
                success: function(result){
                    // console.log(result.code)
                    // console.log(result.data)
                    if(result!=null && result.code==200 && result.data!=null){
                        let userInfo = result.data;
                        //存储token
                        localStorage.setItem("user_token", userInfo.token);
                        localStorage.setItem("login_user_id", userInfo.userId);
                        location.href = "index.html";
                    }else {
                        alert("用户名或密码错误!");
                    }
                },


                error: function (result) {
                    // console.log(result)
                    // console.log(result.responseJSON.data);
                    // console.log(result.responseJSON.code);
                    if (result.responseJSON.code == 502) {
                        alert("用户名或密码输入不正确！");
                    } else if (result.status == 406){
                        alert("用户名或密码输入不能为空！");
                    } else {
                        alert("用户名或密码输入不合法!");
                    }
                }

            });
        }
    </script>
</body>

</html>


